<?php
/***************************************************************************
*                                register.php
 *
 *   Begin date           	: 
 *   Copyright            	: 
 *   Email                	: 
 ***************************************************************************/
defined('PHP5_PHP') or die("Application is stopping!!!");
if(!checkLoginUser()) {
	$msgAlert = $lang['_LOGIN_MSG_'];
	$redirect = sefBuild($php5WebPath, '', 1);
}
require_once($php5RootPath . "/classes/sms_group.class.php");
$row = new SMSGroup($php5DB);
@ini_set('memory_limit', '500M');
$task 		= php5GetParam($_REQUEST, 'task', '');
$smarty->setCompileDir(sprintf($php5RootTempalteCPath, ''));
$tpl = sprintf($php5TemplateFile, $language, 'sms_group', 'create.tpl');
if(!is_file($tpl )) {
	die("can not find template");
}
//list user
$rows_user = array();

//check task
$msg = '';
$noerr = 1;
switch($task)
{
	case 'add':
		$row->bind( $_POST );
		if(!trim($row->name)) {
			$noerr = 0;
			$msg = sprintf($lang['_BLANK_MSG_'], 'Name');
		}
		/////////////////////////check group
		$str = preg_replace( array("/,/", "/_/", "/%/"), array("\,", "\_", "\%"), $row->name);
		$query = "SELECT id "
			. "\n FROM #__sms_group "
			. "\n WHERE name LIKE '" . mysql_real_escape_string($str) . "' AND user_id = '".$php5Session->getVar("user_id")."' LIMIT 1"
			;
		$php5DB->setQuery( $query );
		$checkGroupId = intval($php5DB->loadResult());
		if($checkGroupId) {
			$noerr = 0;
			$msg = sprintf($lang['_EXIST_MSG_'], 'Name');
		}		
		if($noerr) {
			$row->id = 0;
			$row->user_id = $php5Session->getVar("user_id");
			if (!$row->store()) {
				
				$msg = "Error!!! Please try again!!!";		
			} else {
				$msg = $msgAlert = $lang['_UPDATE_MSG_'];
				$redirect = sefBuild($php5WebPath, 'index.php?o=sms_group&m=main', 1);
			}
		}
		break;
	default:
		break;	
		
}
$smarty->assign('msg', '');
if($msg) {
	$smarty->assign('msg', $msg);
}
//assign
$row->list = str_replace("\r\n","\n", $row->list);
$smarty->assign('arrLists', explode("\n",$row->list));
$smarty->assign('list', $row->list);
$smarty->assign('name', $row->name);;
$smarty->assign('link_return', sefBuild($php5WebPath, 'index.php?o=sms_group&m=main', 1));
$smarty->assign('action', sefBuild($php5WebPath, 'index.php?o=sms_group&m=create&task=add', 1));
$smarty->assign('main', $smarty->fetch($tpl));

return;
